package com.itheima.controller;

import com.itheima.pojo.PageResponseResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentPageRequest;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    //学员列表查询
    @GetMapping
    public Result queryAllStudent(StudentPageRequest studentPageRequest) {
        log.info("学员列表查询所需要的参数:{} ",studentPageRequest);
        PageResponseResult<Student> page = studentService.queryAllStudent(studentPageRequest);
        return Result.success(page);
    }

    //添加学员
    @PostMapping
    public Result insertStudent(@RequestBody Student student) {
        log.info("新增的学员信息为: {}",student);
        studentService.insertStudent(student);
        return Result.success();
    }

    /*修改学员信息,1.根据id查询回显*/
    @GetMapping("/{id}")
    public Result queryByIdStudent(@PathVariable Integer id) {
        log.info("需要查询的id为: {}",id);
        Student student = studentService.queryByIdStudent(id);
        return  Result.success(student);
    }

    /*修改学员信息,2.修改学员信息*/
    @PutMapping
    public Result updateStudent(@RequestBody Student student) {
        log.info("修改后的student是: {}", student.toString());
        studentService.updateStudent(student);
        return Result.success();
    }

    /*
    * //违纪处理
    * */
    @PutMapping("/violation/{id}/{score}")
    public Result violationStudent(@PathVariable Integer id,@PathVariable Integer score) {
        log.info("违纪学生的id: {}, 增加的违纪分数是: {}",id,score);
        studentService.violationStudent(id,score);
        return Result.success();
    }

    //删除学员
    @DeleteMapping("/{ids}")
    public Result deleteStudent(@PathVariable List<Integer> ids) {
        log.info("要删除的ids为: {}",ids);
        studentService.deleteStudent(ids);
        return Result.success();
    }

}
